var app = getApp()
Page({
  data: {
    cellphone: '',
    wechatNumber: ''
  },
  onLoad: function (options) {
    var _this = this
    wx.request({
      url: 'https://cake.cindata.cn/contact/getContactByOpenId',
      data: {
        id: app.globalData.openId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      complete: function complete(res) {
        if (res.statusCode == 200) {
          _this.setData({
            cellphone: res.data.cellphone,
            wechatNumber: res.data.wechatNumber
          })
        }
      }
    });
  },
  formSubmit: function (e) {
    var warn = "";
    var flag = true;
    if (e.detail.value.wechatNumber.trim() == "") {
      warn = "请输入您的微信号";
    } else if (!(/^1[0-9]{10}$/.test(e.detail.value.cellphone.trim()))) {
      warn = "手机号码格式不正确";
    } else {
      flag = false;
      wx.request({
        url: 'https://cake.cindata.cn/contact/editContact',
        data: {
          openId: app.globalData.openId,
          wechatNumber: e.detail.value.wechatNumber,
          cellphone: e.detail.value.cellphone
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        complete: function complete(res) {
          if (res.statusCode == 200) {
            wx.navigateBack();
          }
        }
      });
    }  

    //如果信息填写不完整，弹出输入框  
    if (flag == true) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
    }
  }
})